﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DevicesView.Entity;
using Newtonsoft.Json;
using DevicesView.Logic.App;
using BCL.Tool;
namespace DevicesView.Logic.User
{
    public class UserLogic
    {
        private static IUserLogic logic = new XiannuanUser();


        /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        /// <param name="pass"></param>
        /// <param name="appCode"></param>
        /// <returns></returns>

        public int Login(UserConfig user)
        {
            logic.Error = 0;
            SystemUserInfo info = logic.Login(user);
            if (info == null)
                return logic.Error;

            user.UserId = info.Id.ToString();
            user.Save();
            AppResource.CurrentResource.LoginUser = info;
            AppResource.CurrentResource.Save();
            return logic.Error;
        }
    }

    public interface IUserLogic
    {
        int Error { get; set; }
        SystemUserInfo Login(UserConfig user);
        HospitalInfo GetOrg(string orgId);
    }

    /// <summary>
    /// 114服务用户管理
    /// </summary>
    public class XiannuanUser : IUserLogic
    {
        private string httpAdd = App.AppResource.LogicServerAddress;
        public int Error { get; set; }
        public SystemUserInfo Login(UserConfig user)
        {
            SystemUserInfo dy = new SystemUserInfo { LoginName = user.LoginName, Password = user.Password };
            HttpItem hi = new HttpItem { Url = httpAdd + "systemuser/login", Method = HttpMethod.Post, ContentType = "application/json" };
            hi.Content = JsonConvert.SerializeObject(dy).GetBytes(UTF8Encoding.UTF8);
            HttpResult<SystemUserInfo> httpResult = WebLogic.GetHttpResult<SystemUserInfo>(hi);

            if (!httpResult.Success || httpResult.Result == null)
            {
                Error = httpResult.StatusCode == 204 ? 1002 : 65536;
                return null;
            }
            return httpResult.Result;
        }
        public HospitalInfo GetOrg(string orgId)
        {
            HttpItem hi = new HttpItem { Url = httpAdd + "invoke/staticdata", Method = HttpMethod.Post, ContentType = "text/plain", Content = new byte[] { 1 } };
            hi.Url = httpAdd + "invoke/org";
            HttpResult<dynamic> httpresultorg = WebLogic.GetHttpResult<dynamic>(hi);
            HospitalInfo info = null;
            if (httpresultorg.Success && httpresultorg.Result != null || httpresultorg.Result.Id.ToString() != AppResource.CurrentResource.Hospital.Code)
            {
                info = new HospitalInfo();
                dynamic org = httpresultorg.Result;
                info.Code = org.Id.ToString();
                info.Name = org.OrgName;
                info.Phone = org.Telephone;
                info.Address = org.Address;
            }
            return info;
        }
    }

    /// <summary>
    /// warmAPI用户管理   --后期会放弃
    /// </summary>
    public class XiaonuanWarmApiUser
    {
        private string httpAdd = App.AppResource.LogicServerAddress;
        public int Error { get; set; }
        public SystemUserInfo Login(UserConfig user)
        {
            SystemUserInfo dy = new SystemUserInfo { LoginName = user.LoginName, Password = user.Password };
            HttpItem hi = new HttpItem { Url = httpAdd + "systemuser/login", Method = HttpMethod.Post, ContentType = "application/json" };
            hi.Content = JsonConvert.SerializeObject(dy).GetBytes(UTF8Encoding.UTF8);
            HttpResult<SystemUserInfo> httpResult = WebLogic.GetHttpResult<SystemUserInfo>(hi);

            if (!httpResult.Success || httpResult.Result == null)
            {
                Error = httpResult.StatusCode == 204 ? 1002 : 65536;
                return null;
            }
            return httpResult.Result;
        }



        public HospitalInfo GetOrg(string orgId)
        {
            HttpItem hi = new HttpItem { Url = httpAdd + "invoke/staticdata", Method = HttpMethod.Post, ContentType = "text/plain", Content = new byte[] { 1 } };
            hi.Url = httpAdd + "invoke/org";
            HttpResult<dynamic> httpresultorg = WebLogic.GetHttpResult<dynamic>(hi);
            HospitalInfo info = null;
            if (httpresultorg.Success && httpresultorg.Result != null || httpresultorg.Result.Id.ToString() != AppResource.CurrentResource.Hospital.Code)
            {
                info = new HospitalInfo();
                dynamic org = httpresultorg.Result;
                info.Code = org.Id.ToString();
                info.Name = org.OrgName;
                info.Phone = org.Telephone;
                info.Address = org.Address;
            }
            return info;
        }
    }
}
